package com.yoda.youdianda.dto;

import com.yoda.youdianda.common.ErrorResult;
import com.yoda.youdianda.domain.SysMenu;
import com.yoda.youdianda.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;

/**
 * 新增/修改菜单接收dto
 *
 * @author shawn
 * @date 2022年 11月 14日 11:44
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class MenuDto {

    private String id;
    private String pid;
    private String path;
    private String component;
    private String title;
    private String icon;
    private String is_show;
    private String is_cache;
    private String is_link;
    private String redirect;
    private String type;
    private String sort;
    private String mark;


    public static SysMenu init(MenuDto dto) {
        if (Objects.isNull(dto)) {
            throw new BusinessException(ErrorResult.paramNullError());
        }
        SysMenu menu = new SysMenu();
        BeanUtils.copyProperties(dto, menu);
        menu.setPid(Integer.parseInt(dto.getPid()));
        menu.setIsShow(Integer.parseInt(dto.getIs_show()));
        menu.setIsCache(Integer.parseInt(dto.getIs_cache()));
        menu.setIsLink(Integer.parseInt(dto.getIs_link()));
        menu.setSort(Integer.parseInt(dto.getSort()));
        if (!StringUtils.isEmpty(dto.getId())) {
            menu.setId(Integer.parseInt(dto.getId()));
        }
        return menu;
    }

}
